package flc.ast.dialog;

import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemClickListener;

import java.util.ArrayList;
import java.util.List;

import flc.ast.R;
import flc.ast.adapter.NoteAdapter;
import flc.ast.databinding.DialogNoteBinding;
import stark.common.basic.base.BaseSmartDialog;

public class NoteDialog extends BaseSmartDialog<DialogNoteBinding> implements View.OnClickListener {

    private int currentNotes;
    private List<Integer> noteList;
    private NoteAdapter noteAdapter;

    public NoteDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected int getGravity() {
        return Gravity.CENTER;
    }

    public void setNote(int notes) {
        currentNotes = notes;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_note;
    }

    @Override
    protected void initView(View view) {
        mDataBinding.tvNoteCancel.setOnClickListener(this);
        mDataBinding.tvNoteConfirm.setOnClickListener(this);

        noteList = new ArrayList<>();
        noteList.add(1);
        noteList.add(2);
        noteList.add(4);
        noteList.add(8);
        noteList.add(16);

        int startPos = 0;
        for (int i = 0; i < noteList.size(); i++) {
            if (noteList.get(i) == currentNotes) {
                startPos = i;
                break;
            }
        }

        mDataBinding.rvNoteList.setLayoutManager(new GridLayoutManager(getContext(), 5));
        noteAdapter = new NoteAdapter();
        mDataBinding.rvNoteList.setAdapter(noteAdapter);
        noteAdapter.setSelPosition(startPos);
        noteAdapter.setList(noteList);
        noteAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                noteAdapter.setSelPosition(position);
                noteAdapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tvNoteCancel:
                dismiss();
                break;
            case R.id.tvNoteConfirm:
                dismiss();
                if (listener != null) {
                    listener.onClickConfirm(noteList.get(noteAdapter.getSelPosition()));
                }
                break;
        }
    }

    private IListener listener;

    public void setListener(IListener listener) {
        this.listener = listener;
    }

    public interface IListener {
        void onClickConfirm(int note);
    }
}
